---
title: "R Notebook"
output: html_notebook
---



```{r}

library(tidyverse)
library(lubridate)



dk2015 = arrow::read_parquet("danish_election.parquet")
sv2014 = arrow::read_parquet("swedish_election.parquet")
de2017 = arrow::read_parquet("german_election.parquet")
it2018 = arrow::read_parquet( "italian_election.parquet")



create_overview_graph = function(df, election_day, label, dv = "distance_political_hate_facebook", ylabel = "Political hate") {
  
p1 = ggplot(df, aes(x = dato, y = mean_pol_hate_panelist, color = Election, size = size_points)) +
  geom_smooth(formula = y ~ poly(x,20), se = FALSE, method = "lm") +
  geom_point() +
  scale_x_date(breaks = "1 week",date_labels = "%y-%W") +
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5)) +
  xlab("Year-week") +
  ylab(ylabel) +
  geom_vline(xintercept = as.Date(election_day), 
              color = "black", 
              lwd = 1,
             linetype = "dotted") +
  theme_classic() +
  scale_size_manual(values=c(3,1),guide = "none") +
  ggtitle(label) +
  theme(text = element_text(size=15)) 

}




p1 = create_overview_graph(sv2014, "2014-09-14", "Swedish election")
p2 = create_overview_graph(dk2015, "2015-06-18", "Danish election")
p3 = create_overview_graph(de2017, "2017-09-24", "German election")
p4 = create_overview_graph(it2018,"2018-03-04", "Italian election")



all_plot = ggpubr::ggarrange(p1,p2,p3,p4,ncol  = 2, nrow = 2, common.legend = TRUE, legend = "bottom")

ggsave("country_plot_time.png", plot = all_plot, height = 10, width = 15)


```

